package com.sihan.framework.etm.common.websocket;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

import com.framework.web.websocket.WebSocketServer;
import com.sihan.framework.etm.common.PageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.framework.mybatis.Condition;
import com.framework.util.JsonUtils;
import com.sihan.framework.etm.dto.business.BaseDTO;
import com.sihan.framework.etm.dto.business.ScrapReasonDTO;
import com.sihan.framework.etm.entity.User;
import com.sihan.framework.etm.enums.WebSocketFunc;
import com.sihan.framework.etm.mapper.UserMapper;
import com.sihan.framework.etm.service.business.BorrowService;
import com.sihan.framework.etm.service.business.CheckService;
import com.sihan.framework.etm.service.business.ScrapStandingService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

@Component
@Slf4j
public class WebSocketUtil {
	
	@Autowired
	private BorrowService borrowService;
	
	@Autowired
	private CheckService checkService;
	
	@Autowired
	private ScrapStandingService scrapStandingService;
	
	@Autowired
	private UserMapper userMapper;

	/**
	 * 
	 * @description  领用的统计和明细
	 * @auther 韩兵
	 * @Date 2020年8月10日
	 * @return void
	 */
	public void sendMsgForBorrow() {
		WebSocketDispathModel result = new WebSocketDispathModel();
		result.setDispathFunc(WebSocketFunc.BORROW.getValue());
		WebSocketContentModel content = new WebSocketContentModel();
		content.setStatistics(borrowService.selectLastNoDoneBorrowPlan());
		content.setDetail(borrowService.selectBorrowDetailByMasterId(new PageRequest()));
		result.setContent(content);
		try {
			sendMsg(JsonUtils.toJsonString(result));
		} catch (IOException e) {
			log.error("websocket-sendMsgForBorrow-error", e);
		}
	}

	/**
	 * 异常提醒
	 */
	public void sendMsgWarning(String toolId){
		WebSocketDispathModel result = new WebSocketDispathModel();
		result.setDispathFunc(WebSocketFunc.BORROW.getValue());
		WebSocketContentModel content = new WebSocketContentModel();
		content.setStatistics(borrowService.selectLastNoDoneBorrowPlan());
		content.setDetail(borrowService.selectBorrowDetailByMasterId(new PageRequest()));
		result.setContent(content);
		try {
			sendMsg(JsonUtils.toJsonString(result));
		} catch (IOException e) {
			log.error("websocket-sendMsgForBorrow-error", e);
		}
	}


	/**
	 * 
	 * @description 归还的统计和明细
	 * @auther 韩兵
	 * @Date 2020年8月10日
	 * @return void
	 */
	public void sendMsgForBorrowReturn() {
		WebSocketDispathModel result = new WebSocketDispathModel();
		result.setDispathFunc(WebSocketFunc.BORROW_RETURN.getValue());
		WebSocketContentModel content = new WebSocketContentModel();
		content.setStatistics(borrowService.selectReturnStatisticsByUserId());
		content.setDetail(borrowService.selectReturnDetailByMasterId(new PageRequest()));
		result.setContent(content);
		try {
			sendMsg(JsonUtils.toJsonString(result));
		} catch (IOException e) {
			log.error("websocket-sendMsgForBorrowReturn-error", e);
		}
	}
	
	/**
	 * 
	 * @description 检验出库
	 * @auther 韩兵
	 * @Date 2020年8月10日
	 * @return void
	 */
	public void sendMsgForInspectOut() {
		WebSocketDispathModel result = new WebSocketDispathModel();
		result.setDispathFunc(WebSocketFunc.INSPECT_OUT.getValue());
		WebSocketContentModel content = new WebSocketContentModel();
		content.setStatistics(checkService.getCheckToolOutStatistical());
		content.setDetail(checkService.getToolCheckOutList(new BaseDTO()));
		result.setContent(content);
		try {
			sendMsg(JsonUtils.toJsonString(result));
		} catch (IOException e) {
			log.error("websocket-sendMsgForInspectOut-error", e);
		}
	}
	
	/**
	 * 
	 * @description  检验入库
	 * @auther 韩兵
	 * @Date 2020年8月10日
	 * @return void
	 */
	public void sendMsgForInspectIn() {
		WebSocketDispathModel result = new WebSocketDispathModel();
		result.setDispathFunc(WebSocketFunc.INSPECT_IN.getValue());
		WebSocketContentModel content = new WebSocketContentModel();
		content.setStatistics(checkService.getCheckToolInStatistical());
		content.setDetail(checkService.getCheckToolInList(new BaseDTO()));
		result.setContent(content);
		try {
			sendMsg(JsonUtils.toJsonString(result));
		} catch (IOException e) {
			log.error("websocket-sendMsgForInspectIn-error", e);
		}
	}
	
	/**
	 * 
	 * @description 报废清单的加入
	 * @param toolId
	 * @auther 韩兵
	 * @Date 2020年8月6日
	 * @return void
	 */
	public void sendMsgForScrapDetail(String toolId) {
		WebSocketDispathModel result = new WebSocketDispathModel();
		result.setDispathFunc(WebSocketFunc.SCRAAP_DETAIL.getValue());
		ScrapReasonDTO dto = new ScrapReasonDTO();
		dto.setToolId(toolId);
		result.setContent(scrapStandingService.getToolInfoList(dto));
		try {
			sendMsg(JsonUtils.toJsonString(result));
		} catch (IOException e) {
			log.error("websocket-sendMsgForScrapDetail-error", e);
		}
	}
	
	/**
	 * 
	 * @description  获取所有用户
	 * @return
	 * @auther 韩兵
	 * @Date 2020年8月7日
	 * @return List<Long>
	 */
	public List<String> acquiredAllUserIds(){
		List<User> users = userMapper.selectList(new Condition<User>());
		if(null != users && !users.isEmpty()) {
			return users.stream().map(User::getId).collect(Collectors.toList());
		}
		return null;
	}
	
	public void sendMsg(String content) {
		acquiredAllUserIds().stream().forEach(id->{
			try {
				WebSocketServer.sendInfo(content, id);
			} catch (IOException e) {
				log.error("websocket-error", e);
			}
		});
	}
//	public void sendMsg(String content,String id) {
//			try {
//				WebSocketServer.sendInfo(content, id);
//			} catch (IOException e) {
//				log.error("websocket-error", e);
//			}
//		}


	public void sendMsg(String content,String id) {
		try {
			WebSocketSession session = WsSessionManager.get(id);
			session.sendMessage(new TextMessage(content));
		} catch (IOException e) {
			log.error("websocket-error", e);
		}
	}
}
